<template>
  <div class="page_content daraBoardStreetAnalysis_content">
    <div class="header_box p-12 bold flex flex-ac flex-jsb">
      <div class="title flex flex-ac  ">
        <div class="mr-30">角色组管理</div>

      </div>
      <div class="flex flex-ac">
        <el-button @click="handleAllDetailsClick" type="success" size="small" :icon="View" class="mr-10">
          详情
        </el-button>
        <el-button @click="handleAddClick" type="primary" size="small" :icon="Plus">新增</el-button>
        <!-- <el-icon class="add_icon" :size="20" @click="handleAddClick">
            <CirclePlus />
          </el-icon> -->
        <div class="dots flex flex-c flex-jc flex-ac ml-20">
          <span>.</span>
          <span>.</span>
          <span>.</span>
        </div>
      </div>
    </div>
    <div class="main_box m-10 grid">
      <el-table ref="singleTable" :data="tableData" max-height="340" style="width: 100%" fit :header-cell-style="{
          color: '#000',
          background: '#f2f2f2',
        }" tooltip-effect="light">
        <el-table-column type="index" label="序号" align="center" width="55">
        </el-table-column>
        <el-table-column prop="permissionName" label="角色名称" align="center" width="95">
        </el-table-column>
        <el-table-column prop="bz" label="备注" align="center" :show-overflow-tooltip="true">
        </el-table-column>

        <el-table-column prop="default" label="操作" width="200" fixed="right" align="center">
          <template #default="scope">
            <el-button @click="handleDetailsClick(scope.row)" type="success" size="small">
              详情
            </el-button>
            <el-button @click="handleEditClick(scope.row)" type="primary" size="small">编辑</el-button>
            <el-popconfirm width="220" :icon="WarningFilled" icon-color="#f56c6c" title="是否确认删除"
              confirm-button-type="danger" @confirm="handleDelClick(scope.row)" placement="top"
              cancel-button-type="primary" confirm-button-text="确定" cancel-button-text="取消">
              <template #reference>
                <!-- @click="handleDelClick(scope.row)" -->
                <el-button type="danger" size="small">删除</el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 新增 设置 -->
    <el-dialog title="设置 " v-model="dialogAddVisible" width="45%" draggable align-center destroy-on-close
      class="dialog_add" :before-close="handleClose">
      <!-- <el-tabs tab-position="left" class="tabs_with_dots"> -->
      <!-- <el-tab-pane label="基础信息"> -->
      <div class="dialog_addform_box">
        <el-form ref="formRef" :model="form" label-width="90px" class="dialog_addform">
          <el-form-item label="角色名称：" prop="permissionName">
            <el-input v-model="form.permissionName" :disabled="form.disabled" maxlength="20" placeholder="请输入角色名称 "
              clearable style="width: 100%"></el-input>
          </el-form-item>
          <el-form-item label="备注：" prop="bz">
            <el-input v-model="form.bz" :disabled="form.disabled" style="width: 100%" :rows="5" type="textarea"
              placeholder="请输入备注" />
          </el-form-item>
        </el-form>
      </div>
      <!-- </el-tab-pane> -->
      <!-- <el-tab-pane label="数据权限">
          <div class="dialog_addform_box"></div>
        </el-tab-pane>
        <el-tab-pane label="数据看板">
          <div class="dialog_addform_box"></div>
        </el-tab-pane>
        <el-tab-pane label="合同管理">
          <div class="dialog_addform_box"></div>
        </el-tab-pane>
        <el-tab-pane label="收集点管理">
          <div class="dialog_addform_box"></div>
        </el-tab-pane>
        <el-tab-pane label="中转站管理">
          <div class="dialog_addform_box"></div>
        </el-tab-pane>
        <el-tab-pane label="车辆管理">
          <div class="dialog_addform_box"></div>
        </el-tab-pane>
        <el-tab-pane label="资料管理">
          <div class="dialog_addform_box"></div>
        </el-tab-pane>
      </el-tabs> -->
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="handleClose">取消</el-button>
          <el-button type="primary" @click="submitForm" v-if="!form.disabled">确定</el-button>
        </div>
      </template>
    </el-dialog>
    <!-- 详情 -->
    <el-dialog title="角色组详情" v-model="dialogVisible" width="70%" draggable align-center destroy-on-close
      :before-close="handleVisibleClose">

      <el-table ref="singleTable" :data="tableAllData" height="340" max-height="340" style="width: 100%" fit
        :header-cell-style="{
          color: '#000',
          background: '#f2f2f2',
        }">
        <el-table-column type="index" label="序号" align="center" width="55">
        </el-table-column>
        <el-table-column prop="permissionName" label="角色名称" align="center" width="95">
        </el-table-column>
        <el-table-column prop="bz" label="备注" align="center" :show-overflow-tooltip="true">
        </el-table-column>

        <el-table-column prop="default" label="操作" width="200" fixed="right" align="center">
          <template #default="scope">
            <el-button @click="handleDetailsClick(scope.row)" type="success" size="small">
              详情
            </el-button>
            <el-button @click="handleEditClick(scope.row)" type="primary" size="small">编辑</el-button>
            <el-popconfirm width="220" :icon="WarningFilled" icon-color="#f56c6c" title="是否确认删除"
              confirm-button-type="danger" @confirm="handleDelClick(scope.row)" placement="top"
              cancel-button-type="primary" confirm-button-text="确定" cancel-button-text="取消">
              <template #reference>
                <el-button type="danger" size="small">删除</el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination_box mt-20">
        <el-pagination small :current-page.sync="currentPage" :page-size.sync="pageSize" :page-sizes="[10, 20, 30, 50]"
          background layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange"
          @current-change="handleCurrentChange" />
      </div>
    </el-dialog>
  </div>
</template>
<script lang="ts" setup>
import {
  ref,
  computed,
  watch,
  onMounted,
  onBeforeUnmount,
  reactive,
  shallowRef,
  onUnmounted,
} from "vue";
// import { useStore } from "vuex";
import * as echarts from "echarts";
import { ElMessageBox, ElMessage } from "element-plus";
import type { UploadProps, UploadUserFile } from "element-plus";
import {
  WarningFilled,
  Plus,
  Delete,
  Edit,
  Search,
  Share,
  Upload,
  View,
} from "@element-plus/icons-vue";
import { getToken } from "@/utils/auth";
// const store = useStore();
import { getRolePermissionsListApi, rolePermissionsDelete, rolePermissionsEdit, rolePermissionsAdd } from "@/api/resourceManagement";
import { useMousePosition } from "@/hooks/dateSelect";
const { timeType } = useMousePosition();
const iconPng = new URL("@/assets/img/index/pf.png", import.meta.url).href;
const props: any = defineProps({});
onMounted(() => { });

onBeforeUnmount(() => { });
onUnmounted(() => { });
const formRef: any = ref(null);
let dialogVisible = ref(false);
let dialogAddVisible = ref(false);

const rules = {
  permissionName: [
    { required: true, message: '请输入角色名称', trigger: 'blur' }
  ],

  bz: [
    {
      required: true,
      message: "请输入备注",
      trigger: "blur",
    },
  ],
};
let tableData = ref([]);
let tableAllData = ref([]);
let form: any = ref({});
const total = ref(0);
const pageSize = ref(10);
const currentPage = ref(1);
let unitManagement = ref([]);

watch(
  timeType,
  () => {
    init();
  },
  {
    immediate: true,
  }
);

function init() {
  getRolePermissionsList();
}
// 用户组 角色权限 三级分类-分页列表查询
async function getRolePermissionsList() {
  tableData.value = [];
  let info = {
    pageNo: 1,
    pageSize: 10,
  };
  const res = await getRolePermissionsListApi(info);
  console.log(" 用户组 角色权限 三级分类-分页列表查询", res, res?.data);
  if (res?.data?.code != 200) return;
  let { result = {} } = res?.data;
  tableData.value = result?.records || [];
  // console.log(
  //   " 用户组 角色权限 三级分类-分页列表查询",
  //   tableData.value,
  //   result
  // );
}
// 用户组 角色权限 三级分类-分页列表查询
async function getRolePermissionsAllList() {
  tableAllData.value = [];
  let info = {
    pageNo: currentPage.value,
    pageSize: pageSize.value,
  };
  const res = await getRolePermissionsListApi(info);
  console.log(" 用户组 角色权限 三级分类-分页列表查询", res, res?.data);
  if (res?.data?.code != 200) return;
  let { result = {} } = res?.data;
  tableAllData.value = result?.records || [];
  // console.log(
  //   " 用户组 角色权限 三级分类-分页列表查询",
  //   tableData.value,
  //   result
  // );
  total.value = Number(result?.total) || 0;
}
const beforeRemove: UploadProps["beforeRemove"] = (uploadFile, uploadFiles) => {
  return ElMessageBox.confirm(`是否删除${uploadFile.name} ?`).then(
    () => true,
    () => false
  );
};
// 新增
const handleAddClick = () => {
  console.log(234124, form, dialogAddVisible.value);
  dialogAddVisible.value = true;
  form.value = {}
};
// 详情
const handleDetailsClick = (scope: any) => {
  dialogAddVisible.value = true;
  form.value = {
    permissionName: scope.permissionName,
    bz: scope.bz,
    id: scope.id,
    disabled: true
  }
};
// 编辑
const handleEditClick = (scope: any) => {
  console.log('编辑', scope);
  dialogAddVisible.value = true;
  form.value = {
    permissionName: scope.permissionName,
    bz: scope.bz,
    id: scope.id,
    disabled: false
  }
};
// 总的详情
const handleAllDetailsClick = () => {
  dialogVisible.value = true;
  getRolePermissionsAllList();
};

// 关闭
const handleClose = () => {
  form.value = {};
  dialogAddVisible.value = false;
};
// 关闭
const handleVisibleClose = () => {
  handleReact()
  dialogVisible.value = false;
};
// 重置
function handleReact() {
  currentPage.value = 1
  pageSize.value = 10

  getRolePermissionsAllList()
}
// 删除
const handleDelClick = async (scope: any) => {
  let res: any = await rolePermissionsDelete({ id: scope.id });
  if (res?.success || res?.data?.code === 200) {
    ElMessage.success("删除成功!")
    form.value = {};
    formRef.value && formRef.value.resetFields();
    getRolePermissionsList();
    getRolePermissionsAllList()
    handleClose()
  } else ElMessage.warning(res?.msg || res?.data?.message);
};
const submitForm = () => {
  console.log(formRef.value, form.value);
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      delete form.value.disabled
      if (form.value?.id) {
        let res: any = await rolePermissionsAdd(form.value);
        console.log(12321, res);

        if (res?.success || res?.data?.code === 200) {
          ElMessage.success("修改成功!")
          form.value = {};
          formRef.value && formRef.value.resetFields();
          getRolePermissionsList();
          getRolePermissionsAllList()
          handleClose()
        } else ElMessage.warning(res?.msg || res?.data?.message);
      } else {
        let res: any = await rolePermissionsEdit(form.value);
        console.log(12312, res);
        if (res?.success || res?.data?.code === 200) {
          ElMessage.success("添加成功!")
          form.value = {};
          formRef.value && formRef.value.resetFields();
          getRolePermissionsList();
          getRolePermissionsAllList()
          handleClose()
        } else ElMessage.warning(res?.msg || res?.data?.message);
      }
    } else {
      return false;
    }
  })
};
//条数
function handleSizeChange(value: any) {
  pageSize.value = value; console.log('条数', value);
  getRolePermissionsAllList()//调取接口
}
function handleCurrentChange(value: any) {
  //页码发生变化会触发，这个用来触发点击页码时触发的。
  currentPage.value = value; console.log('页码发生变化会触发', value);
  getRolePermissionsAllList()//调取接口

}
</script>

<style lang="scss" scoped>
.daraBoardStreetAnalysis_content {
  height: 400px;

  .header_box {
    .add_icon {
      margin-right: 5px;
      cursor: pointer;
    }
  }

  .main_box {
    overflow-y: auto;
  }

  .el-radio.el-radio--large {
    height: 30px;
  }

  .el-upload__tip {
    font-size: 13px;
    margin-top: -30px;
    margin-left: 100px;
    color: #939191;
  }

  .dialog_add {
    .dialog_addform_box {
      width: 500px;
      overflow-x: auto;

      .dialog_addform {}

      .el-form-item {
        width: 100%;
      }
    }
  }
}
</style>
<style lang="scss">
// .el-tabs__item:hover {
//   color: #0052cc;
// }

.tabs_with_dots .el-tabs__item.is-active {
  color: #0052cc !important;
  font-weight: 550;
}

.tabs_with_dots .el-tabs__item {
  // font-weight: 550 !important;
  color: #172b4d;
  height: 50px;
}

.tabs_with_dots .el-tabs__active-bar {
  background-color: #0052cc;
  height: 12px !important;
  width: 12px !important;
  border-radius: 50% !important;
  margin-top: 16px;
  margin-right: -4px;
}

.tabs_with_dots .el-tabs__nav-scroll {
  background-color: #fff;
  overflow: visible !important;
}

.tabs_with_dots .el-tabs__header {
  margin-bottom: 0;
}

.el-tabs__nav-wrap {
  overflow: visible !important;
}
</style>